package com.cinema.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cinema.bean.Hall;
import com.cinema.bean.Hall;
import com.cinema.bean.Cinema;
import com.cinema.bean.User;
import com.cinema.service.CinemaService;
import com.cinema.service.HallService;
import com.cinema.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("hall")
@CrossOrigin
public class HallController {

    @Autowired
    HallService hallService;
    @Autowired
    CinemaService cinemaService;

    @GetMapping("list")
    public Result getList(){
        List<Hall> list=hallService.list();
        for (Hall item:list){
            Cinema cinema=cinemaService.getById(item.getCinemaId());
            item.setCinema(cinema);
        }
        return Result.success(list);
    }

    @GetMapping("page")
    public Result getPage(Page page, @RequestParam("searchText") String searchText){
        System.out.println(searchText);
        IPage<Hall> result=null;
        if(searchText==null||searchText.trim().isEmpty()){
            result=hallService.page(page);
        }else {
            QueryWrapper wrapper=new QueryWrapper();

            wrapper.like("hall_category",searchText);
            result=hallService.page(page,wrapper);
        }
        return Result.success(result);
    }

    @PostMapping
    public Result add(@RequestBody Hall hall) {
        hallService.save(hall);
        return Result.success("保存成功", hall);
    }

    @DeleteMapping("/{hall_id}")
    public Result delete(@PathVariable("hall_id") Long hallId) {
        boolean b = hallService.removeById(hallId);
        if (b) {
            return Result.success("删除成功");
        } else {
            return Result.fail(201, "删除失败");
        }
    }

    @PutMapping
    public Result edit(@RequestBody Hall hall) {
        boolean b = hallService.updateById(hall);
        if (b) {
            return Result.success("修改成功");
        } else {
            return Result.fail(201, "修改失败");
        }
    }
}